﻿using Shop.Core.Redis;
using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.Core.YOP;
using Shop.IServices;
using Shop.Model.Dto.Block;
using Shop.Model.Entity.Use;
using Shop.Model.View.Block;

namespace Shop.Services;

public class BlockAppService: AbstractService, IBlockAppService
{
    private readonly RepositoryBase<UseCardEntity> _cardRepository;
    private readonly IUserContext _userContext;
    private readonly IRedisProvider _redisProvider;
    private readonly RepositoryBase<UseUser> _userRepository;

    public BlockAppService(RepositoryBase<UseCardEntity> cardRepository,
        IUserContext userContext,
        IRedisProvider redisProvider,
        RepositoryBase<UseUser> userRepository)
    {
        _cardRepository = cardRepository;
        _userContext = userContext;
        _redisProvider = redisProvider;
        _userRepository = userRepository;
    }

    /// <summary>
    /// 银行卡列表
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<List<BlockListVo>> BlockList()
    {
        var data = _cardRepository
            .Where(x => x.IsValid == true && x.User == _userContext.Id)
            .OrderBy("FoundTime DESC")
            .Select(x => new BlockListVo()
            {
                Id = x.Id,
                Bank = x.Bank,
                Card = x.Card
            })
            .ToList();
        return data;
    }

    /// <summary>
    /// 添加银行卡
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult> BlockAdd(BlockAddDto dto)
    {
        var useInfo = await _userRepository.Where(x => x.Id == _userContext.Id).FirstAsync();
        if (useInfo == null)
            return Problem("用户不存在");
        var code = await _redisProvider.StringGetAsync("idc" + useInfo.Tele);
        if (dto.Code != code)
            return Problem("验证码错误");
        await _redisProvider.KeyDelAsync("idc" + useInfo.Tele);

        var bankInfor = YopPay.BankCardIdentification(dto.Card);
        if (bankInfor == null || bankInfor.code != "NOP00000")
            return Problem("银行卡不支持");

        var blockList = await _cardRepository.Where(x => x.User == useInfo.Id && x.IsValid == true).ToListAsync();
        if (blockList.Count >= 10)
            return Problem("已到达卡包上限，无法继续添加");
        if (blockList.Count >= 10)
        {
            var repBlock = blockList.Where(x => x.Card == dto.Card).First();
            if (repBlock != null)
                return Problem("银行卡已存在");
        }
        var blockInfo = new UseCardEntity()
        {
            User = useInfo.Id,
            Card = dto.Card,
            Possessor = dto.Possessor,
            Bank = bankInfor.bankName,
            Coding = bankInfor.bankCode
        };
        await _cardRepository.InsertAsync(blockInfo);
        return new AppResult();
    }

    /// <summary>
    /// 解绑银行卡
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult BlockUntie(long dto)
    {
        var blockInfo = _cardRepository.Where(x => x.Id == dto && x.IsValid == true).First();
        if (blockInfo == null)
            return Problem("银行卡不存在或以解绑");
        if (blockInfo.User != _userContext.Id)
            return Problem("操作失败，用户不匹配");
        blockInfo.IsValid = false;
        blockInfo.UntieTime = DateTime.Now;
        _cardRepository.Update(blockInfo);
        return new AppResult();
    }
}